package com.xkstream.client.presenter.kt;

import com.xkstream.client.api.kt.MyZoneApi
import com.xkstream.client.api.kt.MyZoneService
import com.xkstream.client.base.BaseData
import com.xkstream.client.base.BasePresenterImpl
import com.xkstream.client.entity.kt.BlogListData
import com.xkstream.client.rx.RxHelper
import com.xkstream.client.rx.RxObserver

/**
 * Created by xiaoyi on 2017-11-4.
 */

class MyZonePresenter() : BasePresenterImpl(), MyZoneContract.Presenter {


    var view: MyZoneContract.View? = null
    var myzoneService: MyZoneService? = null

    constructor(view: MyZoneContract.View) : this() {
        this.view = view
        this.myzoneService = MyZoneApi.getInstance().service
    }

    override fun getBlogList(params: Map<String, String>,isDelete :Boolean) {
        view?.showLoading()
        myzoneService?.getBlogList(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object : RxObserver<BaseData<BlogListData>>(this) {
                    override fun onFail(message: String) {
                        view?.hideLoading()
                        view?.showError(message)
                    }

                    override fun onSuccess(t: BaseData<BlogListData>) {
                        view?.hideLoading()
                        view?.showBlogList(t,isDelete)
                    }

                })

    }


    override fun blogUpVote(params: Map<String, String> ) {
        view?.showLoading()
        myzoneService?.blogUpVote(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object : RxObserver<BaseData<Any?>>(this) {
                    override fun onFail(message: String) {
                        view?.hideLoading()
                        view?.showError(message)
                    }

                    override fun onSuccess(t: BaseData<Any?>) {
                        view?.hideLoading()
                        view?.showUpVoteResult(t)
                    }
                })
    }

    override fun deleteBlog(params: Map<String, String>,index :Int) {
       view?.showLoading()
        myzoneService?.deleteBlog(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object :RxObserver<BaseData<Any?>>(this){
                    override fun onSuccess(t: BaseData<Any?>) {
                        view?.hideLoading()
                        view?.showDeleteResult(t,index)
                    }

                    override fun onFail(message: String?) {
                        view?.hideLoading()
                    }
                })
    }

    override fun setZoneBg(params: Map<String, String>) {
        view?.showLoading()
        myzoneService?.setZoneBg(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object :RxObserver<BaseData<Any?>>(this){
                    override fun onSuccess(t: BaseData<Any?>) {
                        view?.hideLoading()
                        view?.showSetZoneBgResult(t)
                    }

                    override fun onFail(message: String?) {
                        view?.hideLoading()
                    }
                })
    }

}
